package plantshop.model;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import plantshop.controller.moves.Move;
import plantshop.controller.moves.MoveRequestThread;
import plantshop.model.plants.Plant;


public class Game implements Serializable{

	private static final long serialVersionUID = 185046175878294827L;

	private static final int START_GAME_TIME = 119000;
	
	private long time;
	private boolean running;
	private Store store;
	private MoveRequestThread moveRequests;
	private long gameTime;
	private int numOpponents;
	
	private static final int START_MONEY = 100;

	public Game(double scalex, double scaley){
		store = new Store(START_MONEY, scalex, scaley);
		CollisionDetector.setStore(store);
		time = 0;
		moveRequests = new MoveRequestThread(this);
		gameTime = START_GAME_TIME;
		this.numOpponents = 1;
	}
	
	public Store getStore(){
		return store;
	}

	public Map<Integer, Move> getNextMoves(){
		return moveRequests.getNextMoves();
	}

	private void setTime(){
		time = System.currentTimeMillis();
	}

	public void movePlant(boolean whereStart, int dispStart, int locStart, boolean whereEnd, int dispEnd, int locEnd){
		Plant p = store.getPlant(whereStart, dispStart, locStart);
		store.removePlant(whereStart, dispStart, locStart);
		store.addPlant(whereEnd, dispEnd, locEnd, p);
	}

	public void buyPlant(int disp, int loc, Plant p){
		store.buyPlant(disp, loc, p);
	}
	
	public void createCreature(Creature c){
		store.createCreature(c);
	}

	public int sellPlant(int disp, int i){
		return store.sellPlant(disp, i);
	}
	
	public int sellRequestedPlant(boolean where, int disp, int i, int dominant, Customer customer)
	{
		return store.sellRequestedPlant(where, disp, i, dominant, customer);
	}
	
	public void markPlant(int disp, int loc){
		store.markPlant(disp, loc);
	}

	public void updateTotalScore(int num){
		store.updateTotalScore(num);
	}

	public int getStoreAttractiveness(){
		return store.getAttractiveness();
	}

	public void sendCustomer(){
		store.sendCustomer();
	}
	public void sendCustomer(Customer c){
		store.sendCustomer(c);
	}
	
	public List<Customer> getCustomers(){
		return store.getCustomers();
	}
	
	public List<Creature> getCreatures(){
		return store.getCreatures();
	}
	
	public void deleteCustomer(Customer c){
		store.removeCustomer(c);
	}
	public void deleteCreature(Creature c){
		store.removeCreature(c);
	}
	
	public void deletePlant(boolean where, int disp, int i){
		store.removePlant(where, disp, i);
	}
	
	public void startGame(){
		running = true;
		setTime();
		moveRequests.setTime(time);
		moveRequests.start();
	}
	
	public boolean isRunning(){
		return running;
	}

	public void endGame(){
		running = false;
	}
	
	public long getGameTime(){
		return gameTime;
	}
	
	public void setGameTime(long time){
		gameTime = time;
	}
	
	public void addOpponent(){
		numOpponents++;
	}
	
	public int getNumOpponents(){
		return numOpponents;
	}
}
